<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :xs="24" :sm="12">
            <a-form-item label="方案名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mannerName', validatorRules.mannerName]" placeholder="请输入方案名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="默认标志" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['defaultToken']" :trigger-change="true" dictCode="yn" placeholder="请选择默认标志"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="换班巡检" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['changeClass']" :trigger-change="true" dictCode="yn" placeholder="请选择换班巡检"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="巡检模式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['patrolModel', validatorRules.patrolModel]" :trigger-change="true" dictCode="patrol_model" placeholder="请选择巡检模式"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="提前预警时间(分钟)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['preWarntime']" placeholder="请输入提前预警时间(分钟)"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="巡检方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['patrolManner', validatorRules.patrolManner]" :trigger-change="true" dictCode="patrol_manner" placeholder="请选择巡检方式"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="投产巡检时间(分钟)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pitchChecktime']" placeholder="请输入投产巡检时间(分钟)"></a-input>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="主数据—巡检方案时段" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesPatrolcheckTimespanTable.loading"
            :columns="mesPatrolcheckTimespanTable.columns"
            :dataSource="mesPatrolcheckTimespanTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesChiefdataPatrolcheckModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          mannerName: {
            rules: [
              { required: true, message: '请输入方案名称!'},
            ]
          },
          patrolModel: {
            rules: [
              { required: true, message: '请输入巡检模式!'},
            ]
          },
          patrolManner: {
            rules: [
              { required: true, message: '请输入巡检方式!'},
            ]
          },
        },
        refKeys: ['mesPatrolcheckTimespan', ],
        tableKeys:['mesPatrolcheckTimespan', ],
        activeKey: 'mesPatrolcheckTimespan',
        // 主数据—巡检方案时段
        mesPatrolcheckTimespanTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '班别',
              key: 'classSort',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '时段',
              key: 'timeSpan',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '开始时间',
              key: 'startTime',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '结束时间',
              key: 'endTime',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/chiefdata/mesChiefdataPatrolcheck/add",
          edit: "/chiefdata/mesChiefdataPatrolcheck/edit",
          mesPatrolcheckTimespan: {
            list: '/chiefdata/mesChiefdataPatrolcheck/queryMesPatrolcheckTimespanByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'mannerName','defaultToken','changeClass','patrolModel','preWarntime','patrolManner','pitchChecktime')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesPatrolcheckTimespan.list, params, this.mesPatrolcheckTimespanTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          mesPatrolcheckTimespanList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'mannerName','defaultToken','changeClass','patrolModel','preWarntime','patrolManner','pitchChecktime'))
     },

    }
  }
</script>

<style scoped>
</style>